<?php

/**
 * Date:16-7-18
 * Author: raymond yulong.chen@xici.com
 */
class HnlAction extends Action
{

    public $educationMap = [1 => '大专以下', 2 => '大专', 3 => '本科', 4 => '硕士', 5 => '博士'];
    public $jobMap = [1 => '计算机/互联网/通信', 2 => '公务员/事业单位', 3 => '教师', 4 => '医生', 5 => '护士', 6 => '空乘人员',
        7 => '生产/工艺/制造', 8 => '商业/服务业/个体经营', 9 => '金融/银行/投资/保险', 10 => '文化/广告/传媒', 11 => '娱乐/艺术/表演', 12 => '律师/法务',
        13 => '教育/培训/管理咨询', 14 => '建筑/房地产/物业', 15 => '酒店/旅游', 16 => '农业', 17 => '在校学生'];
    public $religionMap = [0 => '无宗教信仰', 1 => '佛教', 2 => '道教', 3 => '基督教', 4 => '伊斯兰教', 5 => '其他'];
    public $constellationMap = [1 => '摩羯', 2 => '水瓶', 3 => '双鱼', 4 => '白羊', 5 => '金牛', 6 => '双子', 7 => '巨蟹', 8 => '狮子', 9 => '处女', 10 => '天秤', 11 => '天蝎', 12 => '射手'];
    public $shengxiaoMap = [1 => '鼠', 2 => '牛', 3 => '虎', 4 => '兔', 5 => '龙', 6 => '蛇', 7 => '马', 8 => '羊', 9 => '猴', 10 => '鸡', 11 => '狗', 12 => '猪'];
    public $maritalMap = [1 => '未婚', 2 => '离异无子女', 3 => '离异不带子女', 4 => '离异带子女', 5 => '丧偶无子女', 6 => '丧偶有子女'];
    public $c_maritalMap = [1 => '未婚', 2 => '离异或丧偶'];
    public $c_educationMap = [2 => '大专及以上', 3 => '本科及以上', 4 => '硕士及以上', 5 => '博士及以上'];


    public function getLove()
    {
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer($customerId);
        $weixinModel = M("user_weixin_info");
        Session::switchCustomer(0);

        $uid = Session::$user['id'];
        $agent = M('hongniang_agent')->where(['uid' => $uid])->find();
        if (!$agent) {
            Output::instance()->resultCode(code_user_not_exist);
            return;
        }
        $nickname = $this->_post('nickname');
        $verify = $this->_post('vip');

        $start = $this->_post('start');
        $limit = $this->_post('limit');

        $where = ['agent' => $agent['id']];
        if ($nickname) {
            $where['wx.nickname'] = ['like', '%' . trim($nickname) . '%'];
        }
        if (isset($verify) && in_array($verify, [0, 1])) {
            $where['hn.vip'] = intval($verify);
        }

        $model = M("hongniang_love");
        $result = $model->alias('hn')->join("wx_" . $customerId . "_user_weixin_info wx ON hn.uid = wx.id")->where($where)->field("hn.*,wx.nickname,wx.headimgurl")->order(['createTime' => 'desc'])->limit($start, $limit)->select();
        $count = $model->alias('hn')->join("wx_" . $customerId . "_user_weixin_info wx ON hn.uid = wx.id")->where($where)->count();
        $list = [];
//下列查询可先收集ID再统一查询，时间充裕可优化
        foreach ((array)$result as $item) {
            $item['matchmakerName'] = '';
            $item['matchmakerAvatar'] = '';
            if ($item['matchmakerId']) {
                $row = $weixinModel->where(['id' => $item['matchmakerId']])->find();
                if ($row['nickname']) {
                    $item['matchmakerName'] = $row['nickname'];
                    $item['matchmakerAvatar'] = $row['headimgurl'];
                }
            }
            $item['vipName'] = '';
            if ($item['agent']) {
                $row = M('hongniang_agent')->where(['id' => intval($item['agent'])])->find();
                $item['vipName'] = $row['name'];
            }
            $list[] = $item;
        }
        Session::switchCustomer(0);
        if ($result) {
            Output::instance()->objArray($list, $count);
        } else {
            Output::instance()->resultCode(code_sql_error);
        }
        Session::switchCustomer($customerId);
    }

    public function getLoveDetailById($id)
    {
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        if ($id) {
            $info = M('hongniang_love')->where(['id' => $id])->find();
            $condition = M('hongniang_condition')->where(['uid' => $info['uid']])->find();
            $info['educationTxt'] = $this->educationMap[$info['education']];
            $info['jobTxt'] = $this->jobMap[$info['job']];
            $info['religionTxt'] = $this->religionMap[$info['religion']];
            $info['shengxiaoTxt'] = $this->shengxiaoMap[$info['shengxiao']] ?: '未知';
            $info['constellationTxt'] = $this->constellationMap[$info['constellation']] ?: '未知';
            $info['maritalTxt'] = $this->maritalMap[$info['marital']] ?: '未知';
            if ($condition) {
                $info['c_minAge'] = $condition['maxAge'] ? date('Y') - $condition['maxAge'] : '不限';
                $info['c_maxAge'] = $condition['minAge'] ? date('Y') - $condition['minAge'] : '不限';
                $info['c_minHeight'] = $condition['minHeight'] ?: '不限';
                $info['c_maxHeight'] = $condition['maxHeight'] ?: '不限';
                $info['c_minWeight'] = $condition['minWeight'] ?: '不限';
                $info['c_maxWeight'] = $condition['maxWeight'] ?: '不限';
                $info['c_minEarning'] = $condition['minEarning'] ?: '不限';
                $info['c_maxEarning'] = $condition['maxEarning'] ?: '不限';
                $info['c_areaTxt'] = $condition['area'] ? C('CITY')[$condition['area']] : '不限';
                $info['c_maritalTxt'] = $condition['marital'] ? $this->c_maritalMap[$condition['marital']] : '不限';
                $info['c_educationTxt'] = $condition['education'] ? $this->c_educationMap[$condition['education']] : '不限';
            }
        }
        if ($info) {
            Output::instance()->object($info);
        } else {
            Output::instance()->resultCode(code_sql_error);
        }
    }

    public function loveVerify()
    {
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $result = null;
        $id = $this->_post('id');
        $user = M('hongniang_love')->where(['id' => $id])->find();
        if ($user && $user['vip'] == 1) {
            Output::instance()->resultCode(code_access_denied, "已经核验过了");
        }
        $result = M('hongniang_love')->where(['id' => $id])->save(['vip' => 1]);
        R('Log/add', array('hn', NULL, op_update, '线下核验通过'));
        Output::instance()->resultCode(0, "操作成功");
    }


}